package com.seata.consumer.controller;


import com.seata.interfcae.domain.DemoFirst;
import com.seata.interfcae.domain.DemoSecond;
import com.seata.interfcae.service.GameFirstService;
import com.seata.interfcae.service.GameSecondService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GameController {
    @DubboReference
    private GameFirstService gameFirstService;

    @DubboReference
    private GameSecondService gameSecondService;

    @GetMapping("/game")
    @GlobalTransactional(timeoutMills = 300000, name = "aaa", rollbackFor = Exception.class)
    public String game() {
//      分别插入两条数据
        DemoFirst demoFirst = new DemoFirst();
        demoFirst.setCode("code1");
        demoFirst.setName("name1");
        gameFirstService.game(demoFirst);

        DemoSecond demoSecond = new DemoSecond();
        demoSecond.setCode("code1");
        demoSecond.setName("name1");
        gameSecondService.game(demoSecond);

        return "ok";
    }

    @GetMapping("/gameFail")
    @GlobalTransactional(timeoutMills = 300000, name = "aaa", rollbackFor = Exception.class)
    public String gameFail() {
//      分别插入两条数据
        DemoFirst demoFirst = new DemoFirst();
        demoFirst.setCode("code1");
        demoFirst.setName("name1");
        gameFirstService.game(demoFirst);

        DemoSecond demoSecond = new DemoSecond();
        demoSecond.setCode("code1");
        demoSecond.setName("name1");
        gameSecondService.gameFail(demoSecond);

        return "err";
    }
}
